import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    breads: [],
    user: sessionStorage.getItem('userName'),
    rights: JSON.parse(sessionStorage.getItem('menu')) || [] // 菜单栏
  },
  mutations: {
    // 获取菜单栏
    setRights (state, data) {
      state.rights = data
      // 将登录之后的菜单栏保存到sessionStorage里面
      sessionStorage.setItem('menu', JSON.stringify(data))
    },
    // 将登录之后的用户名保存到sessionStorage里面
    setUser (state, user) {
      state.user = user
      sessionStorage.setItem('userName',user)
    },
    addBread (state, bread) {
      const index = state.breads.findIndex(_bread => _bread.name === bread.name)
      if (index > -1) {
        state.breads.splice(index + 1, state.breads.length - index - 1)
      } else {
        state.breads.push(bread)
      }
    },
    removeBread (state, bread) {
      state.breads = state.breads.filter(_bread => _bread !== bread)
    },
  },
  actions: {
  },
  modules: {
  }
})
